"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】执行触发sql_drop事件触发器，事件在命令执行之后触发
Create At   :   2024/07/20
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0071()
        RETURNS event_trigger LANGUAGE plpgsql AS $$
        DECLARE
        obj record;
        BEGIN
        FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects()
        LOOP
        RAISE NOTICE '% dropped object: % %.% %',
        tg_tag,
        obj.object_type,
        obj.schema_name,
        obj.object_name,
        obj.object_identity;
        END LOOP;
        END;
        $$;
        2.创建表
        create table t_eventtrigger_0071(id int);
    测试步骤：
        1.创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0071 ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0071();
        2.删除表
        drop table t_eventtrigger_0071;
Expect      :
        1.创建事件触发器成功
        2.删除表触发执行函数f_eventtrigger_0071()
        NOTICE:  DROP TABLE dropped object: table public.t_eventtrigger_0071 public.t_eventtrigger_0071
        NOTICE:  DROP TABLE dropped object: type public.t_eventtrigger_0071 public.t_eventtrigger_0071
        NOTICE:  DROP TABLE dropped object: type public._t_eventtrigger_0071 public.t_eventtrigger_0071[]
        删除表成功
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0071;")
        self.comsh.execut_db_sql("drop TABLE if exists t_eventtrigger_0071;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0071();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0071() RETURNS event_trigger LANGUAGE plpgsql AS \$\$ " \
            f"DECLARE " \
            f" obj record; " \
            f"BEGIN " \
            f"FOR obj IN SELECT * FROM pg_event_trigger_dropped_objects() " \
            f"LOOP " \
            f"RAISE NOTICE '% dropped object: % %.% %', " \
            f"tg_tag, " \
            f"obj.object_type, " \
            f"obj.schema_name, " \
            f"obj.object_name, " \
            f"obj.object_identity; " \
            f"END LOOP; " \
            f"END; " \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(f"create table t_eventtrigger_0071(id int);")
        self.logger.info(msg)
        self.assertIn("CREATE TABLE", msg)

    def test_Index_file_damaged(self):
        self.logger.info("--step1:创建事件触发器")
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0071 ON sql_drop EXECUTE PROCEDURE f_eventtrigger_0071();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)
        self.logger.info("--step2:删除表")
        msg = self.comsh.execut_db_sql("drop table t_eventtrigger_0071;")
        self.logger.info(msg)
        self.assertIn(
            f'NOTICE:  DROP TABLE dropped object: table public.t_eventtrigger_0071 public.t_eventtrigger_0071' and \
            f'NOTICE:  DROP TABLE dropped object: type public.t_eventtrigger_0071 public.t_eventtrigger_0071' and \
            f'NOTICE:  DROP TABLE dropped object: type public._t_eventtrigger_0071 public.t_eventtrigger_0071[]' and \
            f"DROP TABLE", msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0071;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0071();")
        self.logger.info(msg)
        self.assertEqual('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
